package com.yeyks.controller.financial;

import com.google.common.collect.Lists;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.param.finance.AddWithdrawalParam;
import com.yeyks.common.appcommon.param.finance.QueryWithdrawalOrderParam;
import com.yeyks.common.appcommon.result.CommonPage;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.exception.OftenFiledException;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.WithdrawalOrderParam;
import com.yeyks.common.param.finance.FinanceWithdrawalParam;
import com.yeyks.common.result.withdrawalorder.AgentWithdrawalOrderResult;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.service.hotel.HotelWithdrawalOrderService;
import com.yeyks.controller.BaseController;
import com.yeyks.hotel.dal.domain.HotelWithdrawalOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;


/**
 * 财务c2b代理模块接口
 *
 * @author Administrator
 */
@Slf4j
@Api(tags = "AppFinanceC2bAgentController财务c2b代理模块接口", description = "财务c2b代理模块接口[c2b代理商申请提现][提现订单查询]")
@RestController
@RequestMapping("/c2b/agent/finance")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppFinanceC2bAgentController extends BaseController {

    private final HotelWithdrawalOrderService hotelWithdrawalOrderService;

    @ApiOperation(value = "c2b代理商申请提现", notes = "成功返回true,失败抛出错误信息")
    @PostMapping("/add/withdrawal")
    public CommonResult<Boolean> withdrawal(@Valid AddWithdrawalParam param) {

        AppAccountDTO loginDTO = validToken(param.getToken(), param.getVersion(), param.getPlatform());

        List<Integer> typeLists = Lists.newArrayList(1, 2, 3, 4);
        Arguments.isTrue(!typeLists.contains(loginDTO.getType()), "您无权调用");

        param.setType(HotelWithdrawalOrder.OrderType.ORDER.value());
        try {

            if (!hotelWithdrawalOrderService.c2bAgentWithdrawal(MyBeanUtils.convert(loginDTO,AccountInfo.class),
                    MyBeanUtils.convert(param, FinanceWithdrawalParam.class))) {
                return CommonResult.failed("新增提现订单异常");
            }
            return CommonResult.success(true);
        }catch (ServiceException e){
            throw new OftenFiledException(e.getMessage());
        }

    }

    @ApiOperation(value = "提现订单查询", notes = "提现订单查询", response = AgentWithdrawalOrderResult.class)
    @PostMapping("/list")
    public CommonResult<CommonPage<AgentWithdrawalOrderResult>> list(@Valid QueryWithdrawalOrderParam param) {

        AppAccountDTO loginDTO = validToken(param.getToken(), param.getVersion(), param.getPlatform());
        param.setOrderType(HotelWithdrawalOrder.OrderType.ORDER.value());
        return hotelWithdrawalOrderService.adminAppC2bAgentList(param, MyBeanUtils.convert(loginDTO, AccountInfo.class));
    }

}
